package com.library;

import java.io.IOException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Event;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.service.AddressLocalServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.liferay.util.portlet.PortletProps;
import com.slayer.model.LMSBook;
import com.slayer.service.LMSBookLocalServiceUtil;

public class LibraryPortlet extends MVCPortlet {

	public void updateBook(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		
		System.out.println("inside updateBook...");
		
		String bookTitle = ParamUtil.getString(actionRequest, "bookTitle");
		String author = ParamUtil.getString(actionRequest, "author");
		long bookId = ParamUtil.getLong(actionRequest, "bookId");
		
		ServiceContext serviceContext = null;
		try {
			serviceContext = ServiceContextFactory.getInstance(actionRequest);
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		LMSBook lmsBook = LMSBookLocalServiceUtil.updateBook(bookId, bookTitle, author, serviceContext);
		
		// save publisher address
		savePublisherAddress(actionRequest, lmsBook);
		
		actionResponse.setRenderParameter("jspPage", "/html/success.jsp");
		actionResponse.setRenderParameter("bookId", String.valueOf(lmsBook.getBookId()));	
	}

	private void savePublisherAddress(ActionRequest actionRequest,
			LMSBook lmsBook) {
		long userId = PortalUtil.getUserId(actionRequest);
		String className = LMSBook.class.getName();
		long classPK = lmsBook.getBookId();
		String street1 = ParamUtil.getString(actionRequest, "street1");
		String street2 = ParamUtil.getString(actionRequest, "street2");
		String city = ParamUtil.getString(actionRequest, "city");
		String zip = ParamUtil.getString(actionRequest, "zip");
		long countryId = ParamUtil.getLong(actionRequest, "countryId");
		long regionId = ParamUtil.getLong(actionRequest, "regionId");
		int typeId = ParamUtil.getInteger(actionRequest, "typeId");
		
		ServiceContext serviceContext = null;
		try {
			serviceContext = ServiceContextFactory.getInstance(actionRequest);
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		try {
			AddressLocalServiceUtil.addAddress(userId, className, classPK, street1,
					street2, StringPool.BLANK, city, zip, regionId, countryId, typeId,
					true, true, serviceContext);
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteBook(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {

		long bookId = ParamUtil.getLong(actionRequest, "bookId");
		
		try {
			LMSBookLocalServiceUtil.deleteLMSBook(bookId);
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
				
		String redirectURL = ParamUtil.getString(actionRequest, "redirectURL");
		actionResponse.sendRedirect(redirectURL);
	}
	
	@Override
	public void render(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {
		
		System.out.println(getInitParameter("max-books"));

		System.out.println(PortletProps.get("book.types"));
		
		String[] bookTypes = PortletProps.getArray("book.types");
		
		for (int i=0; i<bookTypes.length; i++) {
			System.out.println(bookTypes[i]);
		}
		
		super.render(request, response);
	}
	
	public void searchBook(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		
		String searchTerm = ParamUtil.getString(actionRequest, "searchTerm");
				
		List<LMSBook> lmsBooks = LMSBookLocalServiceUtil.searchBook(searchTerm);
		
		actionRequest.setAttribute("SEARCH_RESULT", lmsBooks);
		actionResponse.setRenderParameter("jspPage", "/html/list.jsp");
	}	
	
	public void setPreferences(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {

		String daysToReturn = ParamUtil.getString(actionRequest, "daysToReturn");
		
		PortletPreferences preferences = actionRequest.getPreferences();
		preferences.setValue("days-to-return", daysToReturn);
		preferences.store();
		
		actionResponse.setPortletMode(PortletMode.VIEW);
	}
	
	public void processEvent(EventRequest request, EventResponse response)
			throws PortletException, IOException {

		Event event = request.getEvent();
		
		if (event.getName().equalsIgnoreCase("lmsBook")) {
			LMSBook lmsBook = (LMSBook) event.getValue();
			
			LMSBookLocalServiceUtil.updateBook(0l, lmsBook.getBookTitle(), lmsBook.getAuthor());
		}
	}
	
	public void quickAdd(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {

		String bookTitle = ParamUtil.getString(actionRequest, "bookTitle");
		String author = ParamUtil.getString(actionRequest, "author");
		
		LMSBook lmsBook = LMSBookLocalServiceUtil.updateBook(0l, bookTitle, author);
		actionResponse.setRenderParameter("jspPage", "/html/success.jsp");
		actionResponse.setRenderParameter("bookId", String.valueOf(lmsBook.getBookId()));
		
	}
}